<?php

/**
 * SxUtils
 *
 * @author David Safranek <safrad@rosada.cz>
 */
class SxUtils {
	
	const NOT_AVAILABLE = 'N/A';
	
	/**
	 * 
	 * @param int $bytes
	 * @return string
	 */
	public static function sizeToText3($bytes) {
		if ($bytes >= 1073741824)
		{
				$bytes = number_format($bytes / 1073741824, 2) . ' GiB';
		}
		elseif ($bytes >= 104857600)
		{
				$bytes = number_format($bytes / 1048576, 0) . ' MiB';
		}
		elseif ($bytes >= 10485760)
		{
				$bytes = number_format($bytes / 1048576, 1) . ' MiB';
		}
		elseif ($bytes >= 1048576)
		{
				$bytes = number_format($bytes / 1048576, 2) . ' MiB';
		}
		elseif ($bytes >= 102400)
		{
				$bytes = number_format($bytes / 1024, 0) . ' KiB';
		}
		elseif ($bytes >= 10240)
		{
				$bytes = number_format($bytes / 1024, 1) . ' KiB';
		}
		elseif ($bytes >= 1024)
		{
				$bytes = number_format($bytes / 1024, 2) . ' KiB';
		}
		elseif ($bytes > 1)
		{
				$bytes = $bytes . ' B';
		}
		elseif ($bytes == 1)
		{
				$bytes = $bytes . ' B';
		}
		else
		{
				$bytes = '0 bytes';
		}
		return $bytes;
	}

	/**
	 * 
	 * @param int $bytes
	 * @return string
	 */
	public static function sizeToText2($bytes) {
		if ($bytes >= 1073741824)
		{
				$bytes = number_format($bytes / 1073741824, 1) . ' GiB';
		}
		elseif ($bytes >= 104857600)
		{
				$bytes = number_format($bytes / 1073741824, 2) . ' GiB';
		}
		elseif ($bytes >= 10485760)
		{
				$bytes = number_format($bytes / 1048576, 0) . ' MiB';
		}
		elseif ($bytes >= 1048576)
		{
				$bytes = number_format($bytes / 1048576, 1) . ' MiB';
		}
		elseif ($bytes >= 102400)
		{
				$bytes = number_format($bytes / 1048576, 2) . ' MiB';
		}
		elseif ($bytes >= 10240)
		{
				$bytes = number_format($bytes / 1024, 0) . ' KiB';
		}
		elseif ($bytes >= 1024)
		{
				$bytes = number_format($bytes / 1024, 1) . ' KiB';
		}
		elseif ($bytes >= 100)
		{
				$bytes = number_format($bytes / 1024, 2) . ' KiB';
		}
		elseif ($bytes > 1)
		{
				$bytes = $bytes . ' B';
		}
		elseif ($bytes == 1)
		{
				$bytes = $bytes . ' B';
		}
		else
		{
				$bytes = '0 bytes';
		}
		return $bytes;
	}

	/**
	 * 
	 * @param int $bytes
	 * @return string
	 */
	public static function sizeToText($bytes, $places = 3) {
		switch ($places) {
			case 2: {
				return self::sizeToText2($bytes);
			}
			case 3: {
				return self::sizeToText3($bytes);
			}
			default: {
				new Exception('Invalid parameter.');
			}
		}
	}

	/**
	 * 
	 * @param string $fileName
	 * @return string
	 */
	public static function readStringFromFile($fileName) {
		if (file_exists($fileName)) {
			return file_get_contents($fileName);
		}
	}
	
	/**
	 * 
	 * @param string $fileName
	 * @param string $description
	 * @return string
	 */
	public static function createLink($fileName, $description)
	{
		return self::createLocalLink($fileName, $fileName, $description);
	}

	public static function createLocalLink($fileName, $link, $description)
	{
		$root = './';
		if (file_exists($root . $fileName)) {
			return '<a href="' . $link . '">' . $description . ' (' . self::sizeToText(filesize($root . $fileName), 2) . ')</a>';
		} else {
			//return $description . ' - ' . self::NOT_AVAILABLE;
			return '<span class="strike">' . $description . '</span>';
		}
	}

	/**
	 * Return client IP
	 * i.e. 127.0.0.1
	 * @return string
	 */
	public static function getClientIP() {
		$ip = 0;
		if (!empty($_SERVER['HTTP_CLIENT_IP']))
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ipList = explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
			if ($ip) {
				array_unshift($ipList, $ip);
				$ip = 0;
			}
			foreach ($ipList as $v)
			if (!ereg('^(192\.168|172\.16|10|224|240|127|0)\.',$v))
			return $v;
		}
		return $ip ? $ip : $_SERVER['REMOTE_ADDR'];
	}

	/**
	 * Return file modified date and time as string
	 * i.e. 2014-02-08_16:32:35
	 * @param int $fileName
	 * @return string
	 */
	public static function getFileModified($fileName) {
		if (file_exists($fileName)) {
			return gmdate('Y-m-d_H:i:s', filemtime($fileName));
		} else {
			return '';
		}
	}
	
	public static function mail($to, $subject, $body, $from)
	{
		$headers = "MIME-Version: 1.0\r\n"; 
		$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
		$headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
		$headers .= 'From:' . $from . "\r\n";
		$new_subject = iconv("UTF-8", "ISO-8859-2", $subject);
		$new_subject = "=?ISO-8859-2?B?".base64_encode($new_subject)."?=";
		return mail($to, $new_subject, $body, $headers);
	}

}